package com.jdbc.login.controller;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import com.jdbc.login.service.LoginService;

@WebServlet("/login")
public class LoginServlet extends HttpServlet{
	private static final long serialVersionUID = 1L;
	public LoginServlet(){
		super();
	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		this.doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		resp.setCharacterEncoding("utf-8");
		resp.setContentType("text/html; charset=UTF-8");
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		WebApplicationContext context = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
		LoginService ls= (LoginService) context.getBean("loginServiceImpl");
		boolean b = ls.login(username, password);
		PrintWriter out = resp.getWriter();
		if(b){
			out.println("登陆成功");
		}else{
			out.println("登陆失败");
		}
	}
	
}
